/*
 * Main.java
 *
 * Created on 21 aprile 2007, 12.09
 */

package alecava;

import alecava.interfacce.*;
import java.awt.HeadlessException;
import java.util.*;
import javax.swing.*;
import java.io.*;

/**
 *
 * @author  alecava
 */
public class Main extends javax.swing.JFrame
        implements Updatable<AbstractCavaID3>, Progressable, Settable<String> {
    
    private Vector<AbstractCavaID3> files;
    private final String settingsRadio = "settingsRadio";
    private final String settingsLastPath = "settingsLastPath";
    /** Creates new form Main */
    public Main() {
        initComponents();
        //provvisorio
        labelLyrics.setVisible(false);
        /////////////
        //crea il gruppo dei radioButtons
        ButtonGroup gruppoRadio = new ButtonGroup();
        gruppoRadio.add(radioV1);
        gruppoRadio.add(radioV2);
        //nasconde la progressbar
        this.progressBar.setVisible(false);
        //carica lo stato del radio button e l'ultimo path usato
        Boolean r = Boolean.class.cast(MemoryCard.carica(settingsRadio));
        if (r != null) {
             radioV2.setSelected(!r);
        }
        
        String path = String.class.cast(MemoryCard.carica(settingsLastPath));
        if (path != null) {
            textDir.setText(path);
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        textDir = new javax.swing.JTextField();
        btnSearch = new javax.swing.JButton();
        btnBrowse = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        lista = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        textArtista = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        textTitolo = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        textAlbum = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        textCommento = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        textAnno = new javax.swing.JTextField();
        textTraccia = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        btnScrivi = new javax.swing.JButton();
        checkCommento = new javax.swing.JCheckBox();
        checkArtista = new javax.swing.JCheckBox();
        checkAlbum = new javax.swing.JCheckBox();
        checkGenere = new javax.swing.JCheckBox();
        checkAnno = new javax.swing.JCheckBox();
        textGenere = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        labelLyrics = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        textFiltro = new javax.swing.JTextField();
        labelInfo = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
        jMenuBar1 = new javax.swing.JMenuBar();
        menuStrumenti = new javax.swing.JMenu();
        menuTagger = new javax.swing.JMenuItem();
        menuRinomina = new javax.swing.JMenuItem();
        menuTraccia = new javax.swing.JMenuItem();
        menuEstrai = new javax.swing.JMenuItem();
        menuVersione = new javax.swing.JMenu();
        radioV1 = new javax.swing.JRadioButtonMenuItem();
        radioV2 = new javax.swing.JRadioButtonMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("CavaID3 Editor");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabel1.setText("Directory:");

        btnSearch.setText("Cerca");
        btnSearch.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnSearchMouseClicked(evt);
            }
        });

        btnBrowse.setText("...");
        btnBrowse.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnBrowseMouseClicked(evt);
            }
        });

        lista.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lista.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listaMouseClicked(evt);
            }
        });
        lista.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listaValueChanged(evt);
            }
        });

        jScrollPane1.setViewportView(lista);

        jLabel2.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabel2.setText("Files trovati:");

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel3.setForeground(new java.awt.Color(0, 0, 153));
        jLabel3.setText("Artista");

        textArtista.setBackground(new java.awt.Color(204, 255, 204));

        jLabel4.setForeground(new java.awt.Color(0, 0, 153));
        jLabel4.setText("Titolo");

        textTitolo.setBackground(new java.awt.Color(204, 255, 204));

        jLabel5.setForeground(new java.awt.Color(0, 0, 153));
        jLabel5.setText("Album");

        textAlbum.setBackground(new java.awt.Color(204, 255, 204));

        jLabel6.setForeground(new java.awt.Color(0, 0, 153));
        jLabel6.setText("Commento");

        textCommento.setBackground(new java.awt.Color(204, 255, 204));

        jLabel7.setForeground(new java.awt.Color(0, 0, 153));
        jLabel7.setText("Anno");

        jLabel8.setForeground(new java.awt.Color(0, 0, 153));
        jLabel8.setText("Traccia");

        textAnno.setBackground(new java.awt.Color(204, 255, 204));

        textTraccia.setBackground(new java.awt.Color(204, 255, 204));

        jLabel9.setForeground(new java.awt.Color(0, 0, 153));
        jLabel9.setText("Genere");

        btnScrivi.setText("Scrivi tag");
        btnScrivi.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnScriviMouseClicked(evt);
            }
        });

        checkCommento.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        checkCommento.setMargin(new java.awt.Insets(0, 0, 0, 0));

        checkArtista.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        checkArtista.setMargin(new java.awt.Insets(0, 0, 0, 0));

        checkAlbum.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        checkAlbum.setMargin(new java.awt.Insets(0, 0, 0, 0));

        checkGenere.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        checkGenere.setMargin(new java.awt.Insets(0, 0, 0, 0));

        checkAnno.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        checkAnno.setMargin(new java.awt.Insets(0, 0, 0, 0));

        textGenere.setBackground(new java.awt.Color(204, 255, 204));

        jLabel11.setBackground(new java.awt.Color(204, 255, 255));
        jLabel11.setText(" ? ");
        jLabel11.setToolTipText("Se non sai tra quali generi scegliere cliccami");
        jLabel11.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jLabel11.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel11MouseClicked(evt);
            }
        });

        labelLyrics.setText(" Lyrics ");
        labelLyrics.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        labelLyrics.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                labelLyricsMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelLyrics)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(checkArtista))
                    .addComponent(jLabel4)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(checkAlbum))
                    .addComponent(textTitolo, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
                    .addComponent(textArtista, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
                    .addComponent(textAlbum, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(checkCommento))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(checkGenere))
                    .addComponent(textCommento, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(checkAnno))
                            .addComponent(textAnno, javax.swing.GroupLayout.DEFAULT_SIZE, 58, Short.MAX_VALUE))
                        .addGap(30, 30, 30)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(textTraccia)
                            .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(102, 102, 102))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(textGenere, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel11))
                    .addComponent(btnScrivi, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(12, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(checkCommento))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textCommento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel7)
                                    .addComponent(checkAnno))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(textAnno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel9)
                                    .addComponent(checkGenere)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(textTraccia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel11)
                            .addComponent(textGenere, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(checkArtista))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textArtista, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textTitolo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(checkAlbum))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textAlbum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnScrivi)
                    .addComponent(labelLyrics))
                .addContainerGap())
        );

        jLabel10.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabel10.setText("Filtro:");

        textFiltro.setColumns(10);

        labelInfo.setFont(new java.awt.Font("Dialog", 0, 12));
        labelInfo.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        menuStrumenti.setText("Strumenti");
        menuTagger.setText("Applica tag in massa");
        menuTagger.setToolTipText("Scrive i tag selezionati con una spunta a tutti gli elementi della lista");
        menuTagger.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuTaggerActionPerformed(evt);
            }
        });

        menuStrumenti.add(menuTagger);

        menuRinomina.setText("Rinomina da tag");
        menuRinomina.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuRinominaActionPerformed(evt);
            }
        });

        menuStrumenti.add(menuRinomina);

        menuTraccia.setText("Indovina traccia dal nome del file");
        menuTraccia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuTracciaActionPerformed(evt);
            }
        });

        menuStrumenti.add(menuTraccia);

        menuEstrai.setText("Estrai tag da nome file");
        menuEstrai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuEstraiActionPerformed(evt);
            }
        });

        menuStrumenti.add(menuEstrai);

        jMenuBar1.add(menuStrumenti);

        menuVersione.setText("Formato tag");
        radioV1.setSelected(true);
        radioV1.setText("ID3 v1.1");
        menuVersione.add(radioV1);

        radioV2.setText("ID3 v2.4");
        menuVersione.add(radioV2);

        jMenuBar1.add(menuVersione);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(207, 207, 207))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(textDir, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addComponent(btnBrowse, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(textFiltro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnSearch))
                            .addComponent(jLabel10)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 425, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(labelInfo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnBrowse, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textDir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(btnSearch, 0, 0, Short.MAX_VALUE)
                        .addComponent(textFiltro)))
                .addGap(12, 12, 12)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void menuEstraiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuEstraiActionPerformed
// TODO add your handling code here:
        if (lista.getModel().getSize() > 0){
            TagsFromFileName t = new TagsFromFileName(this.getFiles());
        } else {
            JOptionPane.showMessageDialog(this,"Nessun file nella lista", "Errore", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_menuEstraiActionPerformed
    
    private void menuTracciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuTracciaActionPerformed
// TODO add your handling code here:
        if (this.lista.getModel().getSize() > 0){
            IndovinaTraccia i = new IndovinaTraccia(this.getFiles());
        } else {
            JOptionPane.showMessageDialog(this, "Nessun file nella lista", "Lista vuota", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_menuTracciaActionPerformed
    
    private void labelLyricsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_labelLyricsMouseClicked
// TODO add your handling code here:
        if (lista.getSelectedIndex() != -1){
            Lyrics s = new Lyrics((AbstractCavaID3) this.lista.getSelectedValue());
        } else {
            JOptionPane.showMessageDialog(this, "Non hai selezionato nessun file", "Selezione errata", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_labelLyricsMouseClicked
    
    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
// TODO add your handling code here:
        //scrive lo stato del radio button e dell'ultimo path
        MemoryCard.salva(settingsRadio, radioV1.isSelected());
        MemoryCard.salva(settingsLastPath, textDir.getText());
    }//GEN-LAST:event_formWindowClosing
    
    private void jLabel11MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel11MouseClicked
// TODO add your handling code here:
        GenreSelector s = new GenreSelector(this);
    }//GEN-LAST:event_jLabel11MouseClicked
    
    private Vector<AbstractCavaID3> getFiles(){
        return this.files;
    }
    
    private void menuRinominaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuRinominaActionPerformed
// TODO add your handling code here
        if (lista.getModel().getSize() > 0){
            Vector<AbstractCavaID3> v = this.getFiles();
            /*costruisce il form di rinominazione*/
            FileRenamer r = new FileRenamer(v, this);
        } else
            JOptionPane.showMessageDialog(this, "Nessun file nella lista", "Lista vuota", JOptionPane.ERROR_MESSAGE);
    }//GEN-LAST:event_menuRinominaActionPerformed
    
    private void menuTaggerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuTaggerActionPerformed
        
        if (checkArtista.isSelected() || checkAlbum.isSelected() || checkCommento.isSelected()||
                checkAnno.isSelected() || checkGenere.isSelected()){
            ListModel model = lista.getModel();
            int size = model.getSize();
            if (size > 0){
                /*costruisce le strutture*/
                AbstractCavaID3[] c = new AbstractCavaID3[size];
                String[][] fields = new String[size][7];
                /*le riempe*/
                for (int i = 0; i < size; i++){
                    c[i] = (AbstractCavaID3) model.getElementAt(i);
                    //riempe la matrice con i campi
                    /*sceglie il testo aggiornato se la checkBox è selezionata
                    senò scrive quello che è già scritto nel tag*/
                    fields[i][0] = checkArtista.isSelected() ? textArtista.getText() : c[i].getArtist();
                    fields[i][2] = checkAlbum.isSelected() ? textAlbum.getText() : c[i].getAlbum();
                    fields[i][3] = checkCommento.isSelected() ? textCommento.getText() : c[i].getComment();
                    fields[i][4] = checkAnno.isSelected() ? textAnno.getText() : c[i].getYear();
                    fields[i][5] = checkGenere.isSelected() ? textGenere.getText() : c[i].getGenre();
                    //prende i campi che non ha senso cambiare in massa
                    fields[i][1] = c[i].getTitle();
                    fields[i][6] = c[i].getTrack();
                }
                //lancia il thread
                Thread t = new MassWriter(c, fields, this);
                t.start();
            } else {
                labelInfo.setText("<html><b>Nessun file nella lista</b></html>");
            }
        } else {
            labelInfo.setText("<html><b>Nessun tag selezionato</b></html>");
        }
    }//GEN-LAST:event_menuTaggerActionPerformed
    
    private void btnScriviMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnScriviMouseClicked
// TODO add your handling code here:
        if (lista.getSelectedIndex() != -1) {
            AbstractCavaID3 id = (AbstractCavaID3) lista.getSelectedValue();
            //prende i tag inseriti
            String titolo = textTitolo.getText();
            String artista = textArtista.getText();
            String album = textAlbum.getText();
            String commento = textCommento.getText();
            String anno = textAnno.getText();
            String genere = textGenere.getText();
            String traccia = textTraccia.getText();
            /*scrive*/
            try {
                //scrive il tag
                id.writeTag(artista, titolo, album, commento, anno, genere, traccia);
                //comunica il successo
                labelInfo.setText("<html><b>Tag scritto con successo</b></html>");
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "Errore nella scrittura del file", "Errore IO", JOptionPane.ERROR_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(this, "Nessun file selezionato", "Errore", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnScriviMouseClicked
    
    private void listaValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listaValueChanged
// TODO add your handling code here:
        this.selezioneElemento();
    }//GEN-LAST:event_listaValueChanged
    
    private void listaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listaMouseClicked
        
    }//GEN-LAST:event_listaMouseClicked
    
    private void selezioneElemento() throws HeadlessException {
// TODO add your handling code here:
        if (lista.getSelectedIndex() != -1){
            this.pulisciForm();
            //prende l'oggetto selezionato
            AbstractCavaID3 id = (AbstractCavaID3) lista.getSelectedValue();
            //imposta le info con il percorso completo
            labelInfo.setText(id.getFile().getPath());
            //imposta i campi con i tag
            textTitolo.setText(id.getTitle());
            textArtista.setText(id.getArtist());
            textAlbum.setText(id.getAlbum());
            textCommento.setText(id.getComment());
            textAnno.setText(id.getYear());
            textTraccia.setText(id.getTrack());
            textGenere.setText(id.getGenre());
            
            
        }
    }
    
    private void btnSearchMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnSearchMouseClicked
        popolaLista();
    }//GEN-LAST:event_btnSearchMouseClicked
    
    private void popolaLista() throws HeadlessException {
// TODO add your handling code here:
        boolean versione1 = false;
        if (radioV1.isSelected()){
            versione1 = true;
        }
        File path = new File(textDir.getText());
        /*se il percorso è corretto*/
        if (path.exists() && path.isDirectory()){
            //prende il risultato della ricerca
            Vector<File> v = Ricerca.ricerca(path, textFiltro.getText(), "mp3");
            //istanzia gli oggetti opportuni
            this.files = new Vector<AbstractCavaID3>();
            try{
                for (File f : v){
                    //controllo metto versione 1 o 2???
                    if (versione1)
                        this.files.add(new CavaID3v1(f));
                    else
                        this.files.add(new CavaID3jaudio(f));
                }
            } catch (IOException e) {
                JOptionPane.showMessageDialog(this, "Errore nel caricamento del file ", "Errore IO", JOptionPane.ERROR_MESSAGE);
            }
            //li inserisce nella lista
            lista.setListData(files);
            //pulisce i campi
            pulisciForm();
        } else {
            JOptionPane.showMessageDialog(this,"Devi selezionare una directory di partenza", "Errore", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void pulisciForm() {
        //inizializza anche le caselle di testo
        textTitolo.setText("");
        textArtista.setText("");
        textAlbum.setText("");
        textCommento.setText("");
        textAnno.setText("");
        textTraccia.setText("");
        textGenere.setText("");
        labelInfo.setText("");
    }
    
    private void btnBrowseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnBrowseMouseClicked
// TODO add your handling code here:
        /*prende l'ultima directory aperta*/
        File last = new File(textDir.getText());
        //controlla la correttezza
        if (!last.exists() || !last.isDirectory())
            last = null;
        /************************************/
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setMultiSelectionEnabled(false);
        chooser.setDialogTitle("Seleziona una directory");
        /*se l'ultima directory usata è stata caricata con successo*/
        chooser.setCurrentDirectory(last);
        /*************/
        int ret = chooser.showOpenDialog(this);
        /*se ha selezionato qualcosa*/
        if (ret == chooser.APPROVE_OPTION){
            //imposta la casella di testo con il path
            textDir.setText(chooser.getSelectedFile().getPath());
        }
    }//GEN-LAST:event_btnBrowseMouseClicked
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Main().setVisible(true);
            }
        });
    }
    /*implementa Updatable****************/
    public Vector<AbstractCavaID3> update() {
        popolaLista();
        return this.getFiles();
    }
    /*implementa Progressable**************/
    public void setMax(int x) {
        this.progressBar.setMinimum(0);
        this.progressBar.setMaximum(x-1);
    }
    
    public void setPosition(int x) {
        this.progressBar.setValue(x);
    }
    
    public void start(){
        this.progressBar.setValue(0);
        this.progressBar.setVisible(true);
        this.progressBar.setStringPainted(true);
    }
    
    public void finish(){
        this.progressBar.setVisible(false);
    }
    /*implementa Settable***************/
    public void set(String s){
        this.textGenere.setText(s);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBrowse;
    private javax.swing.JButton btnScrivi;
    private javax.swing.JButton btnSearch;
    private javax.swing.JCheckBox checkAlbum;
    private javax.swing.JCheckBox checkAnno;
    private javax.swing.JCheckBox checkArtista;
    private javax.swing.JCheckBox checkCommento;
    private javax.swing.JCheckBox checkGenere;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelInfo;
    private javax.swing.JLabel labelLyrics;
    private javax.swing.JList lista;
    private javax.swing.JMenuItem menuEstrai;
    private javax.swing.JMenuItem menuRinomina;
    private javax.swing.JMenu menuStrumenti;
    private javax.swing.JMenuItem menuTagger;
    private javax.swing.JMenuItem menuTraccia;
    private javax.swing.JMenu menuVersione;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JRadioButtonMenuItem radioV1;
    private javax.swing.JRadioButtonMenuItem radioV2;
    private javax.swing.JTextField textAlbum;
    private javax.swing.JTextField textAnno;
    private javax.swing.JTextField textArtista;
    private javax.swing.JTextField textCommento;
    private javax.swing.JTextField textDir;
    private javax.swing.JTextField textFiltro;
    private javax.swing.JTextField textGenere;
    private javax.swing.JTextField textTitolo;
    private javax.swing.JTextField textTraccia;
    // End of variables declaration//GEN-END:variables
    
}
